package com.example.demo.dao;

import com.example.demo.entity.Circle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public interface CircleDao extends JpaRepository<Circle,Integer> {

    // 查询列表
    @Query(value = "select c.*,u.nickname,u.avtar,(select status from circle_like cl where cl.circleId = c.id and cl.userId = ?3) as status,(select GROUP_CONCAT(nickname) from user where id in   (select userId from circle_like where circleId = c.id and status = 1)) AS likeUsers from circle c join user u on c.userId=u.id order by time desc limit ?1,?2",nativeQuery = true)
//    @Query(value = "select " +
//            "c.*,u.nickname,u.avtar," +
//            "(select status from circle_like cl where cl.circleId = c.id and cl.userId = ?3) as status," +
//            "(select GROUP_CONCAT(nickname) from user where id in (select userId from circle_like where circleId = c.id and status = 1)) AS likeUsers " +
//            "from circle c join user u on c.userId=u.id order by time desc limit ?1,?2",
//            nativeQuery = true)
    List<Map<String, Objects>> getList(int i, int page, int pageSize);

    // 查询列表
    @Query(value = "select c.*,u.nickname,u.avtar from circle c join user u on c.userId=u.id where c.userId=?3 order by time desc limit ?1,?2",nativeQuery = true)
    List<Map<String, Objects>> getMyList(int page, int pageSize, int userId);

    // 获取用户ID
    List<Circle> findByUserId(int userId);

    // 查询自己点赞过的圈子
    @Query(value = "select c.*,u.nickname,u.avtar from circle c join user u on c.userId=u.id where c.id in (select circleId from circle_like where userId = ?3 and status = 1) order by time desc limit ?1,?2",nativeQuery = true)
    List<Map<String, Objects>> getMyLikeList(int page, int pageSize, int userId);

}
